package com.wzy.mall.web.servlet.admin;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wzy.mall.model.pojo.adminPOJO.UserPOJO;
import com.wzy.mall.model.vo.adminVO.AllUsersVO;
import com.wzy.mall.model.vo.ResultVO;
import com.wzy.mall.service.admin.UserService;
import com.wzy.mall.service.admin.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class User {

    private static ObjectMapper jsonMapper = new ObjectMapper();
    private static UserService userService = new UserServiceImpl();

    /**
     *  查询所有用户信息
     * @param request
     * @param response
     */
    public static void allUsers(HttpServletRequest request, HttpServletResponse response) {
        List<UserPOJO> userPOJOList = userService.selectAllUsers();

        try {
            if(userPOJOList == null || userPOJOList.size() == 0) {
                ResultVO resultVO = new ResultVO(10000, null, "没有找到用户信息哦~");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            } else {
                List<AllUsersVO> allUsersVOList = new ArrayList<AllUsersVO>();
                PojoTOBo(userPOJOList, allUsersVOList);
                ResultVO resultVO = new ResultVO(0, allUsersVOList, null);
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  将POJO对象集合封装成BO对象集合的方法
     * @param userPOJOList
     * @param allUsersVOList
     */
    private static void PojoTOBo(List<UserPOJO> userPOJOList, List<AllUsersVO> allUsersVOList) {
        for (UserPOJO userPOJO : userPOJOList) {
            AllUsersVO allUsersVO = new AllUsersVO();
            allUsersVO.setId(userPOJO.getId());
            allUsersVO.setEmail(userPOJO.getEmail());
            allUsersVO.setNickname(userPOJO.getNickname());
            allUsersVO.setPwd(userPOJO.getPassword());
            allUsersVO.setRecipient(userPOJO.getReceiver());
            allUsersVO.setAddress(userPOJO.getAddress());
            allUsersVO.setPhone(userPOJO.getPhonenumber());

            allUsersVOList.add(allUsersVO);
        }
    }

    /**
     *  根据ID删除用户
     * @param request
     * @param response
     */
    public static void deleteUser(HttpServletRequest request, HttpServletResponse response) {
        // 获取需要删除的用户ID
        String id = request.getParameter("id");

        // 调用Service层 与数据库交互
        int result = userService.deleteUser(Integer.parseInt(id));

        try {
            if(result == 0) {
                ResultVO resultVO = new ResultVO(10000, null, "删除失败啦！！~");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            } else {
                ResultVO resultVO = new ResultVO(0, null, null);
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     *  根据用户昵称查询
     * @param request
     * @param response
     */
    public static void searchUser(HttpServletRequest request, HttpServletResponse response) {
        // 因为请求参数是GET的键值对，所以直接获取即可
        String word = request.getParameter("word");

        // 调用Service层
        List<UserPOJO> userPOJOList = userService.selectByNickname(word);
        System.out.println(userPOJOList);

        try {
            if(userPOJOList == null || userPOJOList.size() == 0) {
                ResultVO resultVO = new ResultVO(10000, null, "没有找到用户信息哦~");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            } else {
                List<AllUsersVO> allUsersVOList = new ArrayList<AllUsersVO>();
                PojoTOBo(userPOJOList, allUsersVOList);

                ResultVO resultVO = new ResultVO(0, allUsersVOList, null);
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
